package types

import (
	

	
)

// Reference: https://www.ietf.org/rfc/rfc4120.txt
// Section: 5.2.2

// PrincipalName implements RFC 4120 type: https://tools.ietf.org/html/rfc4120#section-5.2.2
type PrincipalName struct {
	NameType   int32    `asn1:"explicit,tag:0"`
	NameString []string `asn1:"generalstring,explicit,tag:1"`
}

// NewPrincipalName creates a new PrincipalName from the name type int32 and name string provided.
func ( int32,  string) PrincipalName {
	return PrincipalName{
		NameType:   ,
		NameString: strings.Split(, "/"),
	}
}

// GetSalt returns a salt derived from the PrincipalName.
func ( PrincipalName) ( string) string {
	var  []byte
	 = append(, ...)
	for ,  := range .NameString {
		 = append(, ...)
	}
	return string()
}

// Equal tests if the PrincipalName is equal to the one provided.
func ( PrincipalName) ( PrincipalName) bool {
	if len(.NameString) != len(.NameString) {
		return false
	}
	//https://tools.ietf.org/html/rfc4120#section-6.2 - the name type is not significant when checking for equivalence
	for ,  := range .NameString {
		if .NameString[] !=  {
			return false
		}
	}
	return true
}

// PrincipalNameString returns the PrincipalName in string form.
func ( PrincipalName) () string {
	return strings.Join(.NameString, "/")
}

// ParseSPNString will parse a string in the format <service>/<name>@<realm>
// a PrincipalName type will be returned with the name type set to KRB_NT_PRINCIPAL(1)
// and the realm will be returned as a string. If the "@<realm>" suffix
// is not included in the SPN then the value of realm string returned will be ""
func ( string) ( PrincipalName,  string) {
	if strings.Contains(, "@") {
		 := strings.Split(, "@")
		 = [len()-1]
		 = strings.TrimSuffix(, "@"+)
	}
	 = NewPrincipalName(nametype.KRB_NT_PRINCIPAL, )
	return
}